//导入vue
import Vue from "vue"

//导入路由
import VueRouter from "vue-router"
Vue.use(VueRouter);

import router from "./router.js"

//导入vuex
import Vuex from "vuex"
Vue.use(Vuex)

// 导入axios
import axios from "axios"
axios.defaults.baseURL = "http://www.liulongbin.top:3005/"
Vue.prototype.axios = axios

//导入mui
import "./lib/mui/css/mui.min.css"
import "./lib/mui/css/icons-extra.css"
import "./lib/mui/js/mui.js"

//导入mint

import "mint-ui/lib/style.css"
import MintUi from "mint-ui"
Vue.use(MintUi)

// import { Header, Button } from "mint-ui"
// Vue.component(Header.name, Header);
// Vue.component(Button.name, Button)
// import { Swipe, SwipeItem } from 'mint-ui';
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
//
// import { Lazyload } from 'mint-ui';
// Vue.use(Lazyload);


//导入vue-preview
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

//导入app组件
import app from "./App.vue"

import moment from "moment"

Vue.filter("timeFormat", function(str, pattern="YYYY-MM-DD hh:mm:ss"){
    return moment(str).format(pattern)
})

//每次加载网页 先去加载本地存储里的数据
let car = JSON.parse(localStorage.getItem("car") || [])


//实例化vuex
let store = new Vuex.Store({
    state:{
        car: car
    }
    ,
    mutations:{
        addshopcar(state,list){
            let flag = false

            state.car.some( value=>{
                if(list.id === value.id){
                    value.count  += parseInt(list.count)
                    flag = true
                    return true
                }
            })
            if(!flag){
                state.car.push(list)
            }

            //把car中的数据存储到localstorage
            localStorage.setItem("car", JSON.stringify(state.car))
        }
    },
    getters:{
        getallcount(state){
            let num = 0
            state.car.map(value=>{
                num += value.count
            })
            return num
        },
        getcount(state){
            let o = {}
            state.car.forEach(item=>{
                o[item.id] = item.count //不能写成 o.item.id
            })
            return o
        }
    }
})


let vm = new Vue({
    el: "#app",
    render: c=>c(app),
    router,
    store
})
